<html>
<head><meta charset="utf-8"><title>GetMutRefs · t-libs · Zulip Chat Archive</title></head>
<h2>Stream: <a href="https://rust-lang.github.io/zulip_archive/stream/219381-t-libs/index.html">t-libs</a></h2>
<h3>Topic: <a href="https://rust-lang.github.io/zulip_archive/stream/219381-t-libs/topic/GetMutRefs.html">GetMutRefs</a></h3>

<hr>

<base href="https://rust-lang.zulipchat.com">

<head><link href="https://rust-lang.github.io/zulip_archive/style.css" rel="stylesheet"></head>

<a name="229914553"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/219381-t-libs/topic/GetMutRefs/near/229914553" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Sebastian Malton <a href="https://rust-lang.github.io/zulip_archive/stream/219381-t-libs/topic/GetMutRefs.html#229914553">(Mar 11 2021 at 19:53)</a>:</h4>
<p>Hello <span class="user-mention" data-user-id="125270">@scottmcm</span> on internals told me to write up a mini-rfc and post it here for feedback.</p>
<p>The basic idea of my idea is to introduce a standard (by being in the stdlib) way for data structures to expose a safe mechanism to borrow multiple "elements" from them.</p>
<p>Here is it rendered:</p>
<p><a href="https://github.com/Nokel81/rfcs/blob/get-mut-refs/text/0000-get-mut-refs.md">https://github.com/Nokel81/rfcs/blob/get-mut-refs/text/0000-get-mut-refs.md</a></p>



<a name="229915235"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/219381-t-libs/topic/GetMutRefs/near/229915235" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Amanieu <a href="https://rust-lang.github.io/zulip_archive/stream/219381-t-libs/topic/GetMutRefs.html#229915235">(Mar 11 2021 at 19:57)</a>:</h4>
<p>You might be interested in <a href="https://github.com/rust-lang/hashbrown/pull/239">https://github.com/rust-lang/hashbrown/pull/239</a> which adds similar functionality to hashbrown.</p>



<a name="229920190"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/219381-t-libs/topic/GetMutRefs/near/229920190" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Sebastian Malton <a href="https://rust-lang.github.io/zulip_archive/stream/219381-t-libs/topic/GetMutRefs.html#229920190">(Mar 11 2021 at 20:27)</a>:</h4>
<p>That is indeed interesting. I see that it was chosen to use <code>[Result&lt;&amp;mut T, E&gt;; N]</code> instead of <code>Option&lt;[&amp;mut T; N]&gt;</code>. I wonder if that would be better in general or not.</p>



<a name="229936008"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/219381-t-libs/topic/GetMutRefs/near/229936008" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Mara <a href="https://rust-lang.github.io/zulip_archive/stream/219381-t-libs/topic/GetMutRefs.html#229936008">(Mar 11 2021 at 22:07)</a>:</h4>
<p><span class="user-mention" data-user-id="247082">@Sebastian Malton</span> note that the array <code>[..2, 2..]</code> can't exist. <code>..2</code> and <code>2..</code> have different types (<code>RangeTo</code> and <code>RangeFrom</code>).</p>



<a name="229937119"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/219381-t-libs/topic/GetMutRefs/near/229937119" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> scottmcm <a href="https://rust-lang.github.io/zulip_archive/stream/219381-t-libs/topic/GetMutRefs.html#229937119">(Mar 11 2021 at 22:12)</a>:</h4>
<p>Definitely an interesting scoping question for whether this could ever support <code>.multi_index((..k, k, k+1..)) -&gt; (&amp;mut [T], &amp;mut T, &amp;mut [T])</code> in future...</p>



<a name="229939993"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/219381-t-libs/topic/GetMutRefs/near/229939993" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Sebastian Malton <a href="https://rust-lang.github.io/zulip_archive/stream/219381-t-libs/topic/GetMutRefs.html#229939993">(Mar 11 2021 at 22:29)</a>:</h4>
<p><span class="user-mention" data-user-id="310399">@Mara</span> Thanks for the pointer. But I think it is possible to make <code>[&amp;(..2), &amp;(2..)]</code> work with dynamic dispatch. However I acknowledge that this would be suboptimal.</p>



<a name="229940219"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/219381-t-libs/topic/GetMutRefs/near/229940219" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Sebastian Malton <a href="https://rust-lang.github.io/zulip_archive/stream/219381-t-libs/topic/GetMutRefs.html#229940219">(Mar 11 2021 at 22:31)</a>:</h4>
<p><span class="user-mention" data-user-id="125270">@scottmcm</span> If the <code>keys</code> argument was made to be generic over its type then even before variadic generics or variadic tuples gets implemented it might be possible to implement tuple binding up to 12 items.</p>



<a name="229943869"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/219381-t-libs/topic/GetMutRefs/near/229943869" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> bstrie <a href="https://rust-lang.github.io/zulip_archive/stream/219381-t-libs/topic/GetMutRefs.html#229943869">(Mar 11 2021 at 23:02)</a>:</h4>
<p>is <code>x.get_mut_refs([..2, 2..])</code> not already supported by <code>x.split_at_mut(2)</code>?</p>



<a name="229948210"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/219381-t-libs/topic/GetMutRefs/near/229948210" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Sebastian Malton <a href="https://rust-lang.github.io/zulip_archive/stream/219381-t-libs/topic/GetMutRefs.html#229948210">(Mar 11 2021 at 23:39)</a>:</h4>
<p>That specific case is yes but this would be a more general solution and hopefully more powerful. For instance doing <code>(..k, k, k+1..)</code></p>



<a name="229949239"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/219381-t-libs/topic/GetMutRefs/near/229949239" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> scottmcm <a href="https://rust-lang.github.io/zulip_archive/stream/219381-t-libs/topic/GetMutRefs.html#229949239">(Mar 11 2021 at 23:48)</a>:</h4>
<p>You might want to do a section about whether the general thing should support ranges and such, or whether the general one should just be scalars -- there's some interesting things to consider there, like HashMap only doing scalars, VecDeque not really having a great range option, but BTreeMap and slices both being able to do interesting things.</p>
<p>Typing that also makes me wonder about phrasing things in iterators, and letting people use <code>slice::IterMut::into_slice</code> if needed.</p>



<a name="229955159"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/219381-t-libs/topic/GetMutRefs/near/229955159" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Sebastian Malton <a href="https://rust-lang.github.io/zulip_archive/stream/219381-t-libs/topic/GetMutRefs.html#229955159">(Mar 12 2021 at 00:43)</a>:</h4>
<p>That is a very good question, it could be possible to have keys be more generic to support the ability to have both but I'll consider this a bit more and expand a section</p>



<a name="229963631"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/219381-t-libs/topic/GetMutRefs/near/229963631" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> bstrie <a href="https://rust-lang.github.io/zulip_archive/stream/219381-t-libs/topic/GetMutRefs.html#229963631">(Mar 12 2021 at 02:13)</a>:</h4>
<p>for the slice case, are slice patterns expected to subsume this API eventually? I can imagine <code>if let [..2 @ a, b, 3.. @ c]</code> working someday</p>



<a name="230008910"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/219381-t-libs/topic/GetMutRefs/near/230008910" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Giacomo Stevanato <a href="https://rust-lang.github.io/zulip_archive/stream/219381-t-libs/topic/GetMutRefs.html#230008910">(Mar 12 2021 at 10:49)</a>:</h4>
<p>That would be nice to have, but it is usable only for indexes that are known only at compile time.</p>



<a name="230074365"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/219381-t-libs/topic/GetMutRefs/near/230074365" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> bstrie <a href="https://rust-lang.github.io/zulip_archive/stream/219381-t-libs/topic/GetMutRefs.html#230074365">(Mar 12 2021 at 18:14)</a>:</h4>
<p>actually I've just asked and that use of slice patterns is probably not ever going to be supported; any range pattern other than <code>..</code> is interpreted as matching the contents of the array rather than the indices of the array</p>



<a name="230256073"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/219381-t-libs/topic/GetMutRefs/near/230256073" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Sebastian Malton <a href="https://rust-lang.github.io/zulip_archive/stream/219381-t-libs/topic/GetMutRefs.html#230256073">(Mar 14 2021 at 17:29)</a>:</h4>
<p>Ah thanks for the clarification</p>



<a name="230269386"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/219381-t-libs/topic/GetMutRefs/near/230269386" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Sebastian Malton <a href="https://rust-lang.github.io/zulip_archive/stream/219381-t-libs/topic/GetMutRefs.html#230269386">(Mar 14 2021 at 21:29)</a>:</h4>
<p><span class="user-mention" data-user-id="256342">@bstrie</span> Would you be able to point me to that source so that I can add it to my document?</p>



<a name="230269392"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/219381-t-libs/topic/GetMutRefs/near/230269392" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Sebastian Malton <a href="https://rust-lang.github.io/zulip_archive/stream/219381-t-libs/topic/GetMutRefs.html#230269392">(Mar 14 2021 at 21:29)</a>:</h4>
<p>Thanks</p>



<a name="230403886"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/219381-t-libs/topic/GetMutRefs/near/230403886" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Sebastian Malton <a href="https://rust-lang.github.io/zulip_archive/stream/219381-t-libs/topic/GetMutRefs.html#230403886">(Mar 15 2021 at 19:28)</a>:</h4>
<p>I have just updated the doc following the suggestions from here: <a href="https://github.com/Nokel81/rfcs/blob/get-mut-refs/text/0000-get-mut-refs.md">https://github.com/Nokel81/rfcs/blob/get-mut-refs/text/0000-get-mut-refs.md</a></p>
<p>It would be great if I could get another look over by all of you. And either some more feedback or maybe I should post it as an actual RFC? Thanks.</p>



<a name="230411288"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/219381-t-libs/topic/GetMutRefs/near/230411288" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> bstrie <a href="https://rust-lang.github.io/zulip_archive/stream/219381-t-libs/topic/GetMutRefs.html#230411288">(Mar 15 2021 at 20:16)</a>:</h4>
<p><span class="user-mention silent" data-user-id="247082">Sebastian Malton</span> <a href="#narrow/stream/219381-t-libs/topic/GetMutRefs/near/230269386">said</a>:</p>
<blockquote>
<p><span class="user-mention silent" data-user-id="256342">bstrie</span> Would you be able to point me to that source so that I can add it to my document?</p>
</blockquote>
<p>Not exactly authoritative, but: <a href="#narrow/stream/213817-t-lang/topic/inclusive.20range.20patterns/near/229999069">https://rust-lang.zulipchat.com/#narrow/stream/213817-t-lang/topic/inclusive.20range.20patterns/near/229999069</a> . It appears to be an unavoidable consequence of the fact that <code>x..=y</code> is already stable in pattern contexts for matching a range of items, and <code>[foo]</code> is already stable for matching an array of a single item, so combined <code>[x..=y]</code> matches a length-1 array whose only item is contained within that range</p>



<a name="230411578"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/219381-t-libs/topic/GetMutRefs/near/230411578" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> bstrie <a href="https://rust-lang.github.io/zulip_archive/stream/219381-t-libs/topic/GetMutRefs.html#230411578">(Mar 15 2021 at 20:18)</a>:</h4>
<p>(half-open ranges and inclusive ranges aren't yet stable, but I don't see how anyone could justify giving inclusive and exclusive ranges different semantics here; at best to support this you'd need to deprecate the current support for inclusive ranges in patterns. from this perspective, it's <code>..</code> that's the odd one out)</p>



<a name="230421400"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/219381-t-libs/topic/GetMutRefs/near/230421400" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Sebastian Malton <a href="https://rust-lang.github.io/zulip_archive/stream/219381-t-libs/topic/GetMutRefs.html#230421400">(Mar 15 2021 at 21:27)</a>:</h4>
<p><span class="user-mention" data-user-id="256342">@bstrie</span> and to be frank <code>..</code> is rather useless in this context (which is why I didn't mention it in my revised docs).</p>



<a name="233022546"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/219381-t-libs/topic/GetMutRefs/near/233022546" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Sebastian Malton <a href="https://rust-lang.github.io/zulip_archive/stream/219381-t-libs/topic/GetMutRefs.html#233022546">(Apr 03 2021 at 18:16)</a>:</h4>
<p><span class="user-mention" data-user-id="125270">@scottmcm</span> With regards to your comment on the RFC PR (<a href="https://github.com/rust-lang/rfcs/pull/3100#issuecomment-811467098">https://github.com/rust-lang/rfcs/pull/3100#issuecomment-811467098</a>). I took a dive into reading the code of <code>BTreeMap</code>. I think that I have a grasp on how the internals work enough to get an impl into the document. However, I am slightly unsure as to if I can use <code>idx</code> in <code>node::Handle</code> as a means of differentiating ZSTs. I think I can.</p>



<hr><p>Last updated: Aug 07 2021 at 22:04 UTC</p>
</html>